package adventuregame;

import java.util.ArrayList;

/**
 * Create an instance of AdventureGame.
 * @author Jesper & Simon
 */
public class Game {

	ArrayList<Room> roomList = new ArrayList<Room>();
	ArrayList<String> doorList = new ArrayList<String>();
	ArrayList<Boolean> checkList = new ArrayList<Boolean>();
	ArrayList<String> itemNames = new ArrayList<String>();
	Console console = new Console();
	Config cfg = new Config();
	Person gamer = null;
	Menu m = null;
	boolean running = true;

	/**
	 * Starts game and continues while running is true.
	 */
	public void run() {
		start();
		while (running && m.running) {
			continueGame();
		}
	}

	/**
	 * Prints brief game description and calls setCheckList.
	 * Prints number of items available.
	 * Calls setLists with doors and rooms.
	 * Intialises a new person called gamer, and a new menu called m.
	 */
	public void start() {
		console.println("Welcome to adventure game");
		console.println("Your goal is to have one of each item in your (Base)");
		setCheckList();
		console.println("*Note: there is " + itemNames.size() + " items total");
		setLists("doors");
		setLists("rooms");
		gamer = new Person(roomList.get(0));
		m = new Menu(roomList, gamer);
	}

	/**
	 * Show menu, and call checkIfWon.
	 */
	public void continueGame() {
		m.show();
		checkIfWon();
	}

	/**
	 * Add rows from a commaseperated property to a already intialized
	 * ArrayList depending on type.
	 * @param type String doors or rooms
	 */
	public void setLists(String type) {

		String[] tempArray = cfg.getProperty(type).split(",");
		int max = tempArray.length - 1;
		for (int i = 0; i <= max; i++) {

			if (type.equals("doors")) {
				doorList.add(i, tempArray[i]);
			} else if (type.equals("rooms")) {
				roomList.add(i, new Room(tempArray[i]));
			}

		}
	}

	/**
	 * Check and set running to false if game is won,
	 * and output game completed.
	 */
	private void checkIfWon() {
		if (isWon()) {
			console.println("\nGame Completed!");
			running = false;
		}
	}

	/**
	 * Returns true if checkList only contains true values.
	 * @return boolean
	 */
	private boolean isWon() {
		checkIfItemIsInBaseroom();
		int max = checkList.size();
		for (int i = 0; i < max; i++) {
			if (!checkList.get(i)) {
				return checkList.get(i);
			}
		}
		return true;
	}

	/**
	 * Sets a true value for each thing type which is located in baseroom.
	 */
	private void checkIfItemIsInBaseroom() {
		int max = itemNames.size();
		for (int i = 0; i < max; i++) {
			checkList.set(i, false);
			int tempMax = baseRoom().things.size();
			for (int x = 0; x < tempMax; x++) {
				if (baseRoom().things.get(x).toString().equals(itemNames.get(i))) {
					checkList.set(i, true);
				}
			}
		}
	}

	/**
	 * Get baseroom from all rooms.
	 * @return baseroom Room which is set to base in the property file
	 */
	private Room baseRoom() {
		int max = roomList.size();
		for (int i = 0; i < max; i++) {
			if (roomList.get(i).nameAddition.equals("(base)")) {
				return roomList.get(i);
			}
		}
		return roomList.get(0);
	}

	/**
	 * Makes two ArrayLists.
	 * One (itemNames) containing the names of each item.
	 * The other (checkList) containg a boolean value of each item.
	 */
	private void setCheckList() {
		String[] thingsArray = cfg.getProperty("things").split(",");
		int thingsMax = thingsArray.length - 1;
		for (int x = 0; x <= thingsMax; x++) {
			checkList.add(x, false);
			itemNames.add(x, thingsArray[x]);
		}
	}
}
